package mall.dao.impl;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import mall.bean.Order;
import mall.dao.OrderDao;
import mall.dao.handler.column.UNDERLINE;

public class OrderDaoImpl implements OrderDao {
	
	@Override
	public void save(Order obj)  {
		String sql = "INSERT INTO `order` (uid, create_time, total_amount, state) VALUES (?, ?, ?, ?)";
		JdbcTemplate.update(sql, obj.getUid(), obj.getCreateTime(), obj.getTotalAmount(), obj.getState().getValue());
	}

	@Override
	public void delete(Long id)  {
		String sql = "DELETE FROM `order` WHERE oid = ?";
		JdbcTemplate.update(sql, id);
	}

	@Override
	public void update(Order obj)  {
		String sql = "UPDATE `order` SET total_amount = ?, state = ? WHERE oid = ?";
		JdbcTemplate.update(sql, obj.getTotalAmount(), obj.getState().getValue(), obj.getOid());
	}

	@Override
	public Order findById(Long id)  {
		String sql = "SELECT oid, uid, create_time, total_amount, state FROM `order` WHERE oid = ?";
		return JdbcTemplate.query(new BeanHandler<Order>(Order.class, UNDERLINE.PROCESSOR), sql, id);
	}

	@Override
	public long count()  {
		String sql = "SELECT COUNT(1) FROM `order`";
		return JdbcTemplate.query(new ScalarHandler<Long>(), sql);
	}

}
